package com.chenailin.www.controller;

import com.chenailin.www.pojo.Student;
import com.chenailin.www.service.StudentService;
import com.chenailin.www.util.InputValidator;
import com.chenailin.www.util.MenuUtil;

import java.util.Objects;
import java.util.Scanner;

public class StudentController {
    private final StudentService studentService = new StudentService();
    private final Scanner sc = new Scanner(System.in);

    // 修改学生个人信息
    public void modifyStudent(Student student) {
        System.out.println("\n=== 修改学生 ===");
        System.out.print("请输入新的学生姓名（留空不修改）：");
        String name = sc.nextLine();
        System.out.print("请输入新的学生电话号码（留空不修改）：");
        String tel = sc.nextLine();
        System.out.print("请输入新的学生年龄（填0不修改）：");
        int age = MenuUtil.getValidChoice(0,50);
        try {
            Student original = studentService.getStudentDetail(student.getStuId());
            Student updated = new Student();
            updated.setStuId(student.getStuId());
            updated.setStuName(name.isEmpty() ? original.getStuName() : name);
            updated.setStuTel(tel.isEmpty() ? original.getStuTel() : tel);
            updated.setStuAge(age == 0 ? original.getStuAge() : age);
            if(studentService.modifyStudent(updated)) {
                System.out.println("\n学生修改成功！");
            } else {
                System.out.println("修改失败，请检查输入数据");
            }
        } catch (Exception e) {
            System.out.println("修改失败：" + e.getMessage());
        }
    }

    // 身份验证
    public void verifyStudent(Student student) {
        System.out.println("\n=== 身份验证 ===");
        if (student.getVerified()) {
            System.out.println("已验证，无需重新验证");
            return;
        }
        System.out.println("请输入你的学号(即登录id)：");
        int id = sc.nextInt();
        try {
            if (id != student.getStuId()) {
                System.out.println("学号不一致");
                return;
            }
            if(studentService.verifyStudent(id)) {
                student.setVerified(true);
                System.out.println("\n学生验证成功！");
            } else {
                System.out.println("修改失败，请检查输入数据");
            }
        } catch (Exception e) {
            System.out.println("修改失败：" + e.getMessage());
        }
    }

    // 修改密码
    public void modifyPassword(Student student) {
        System.out.println("\n=== 修改密码 ===");
        System.out.println("请输入旧密码:");
        String oldPassword = sc.nextLine();
        if (!Objects.equals(oldPassword, student.getPassword())) {
            System.out.println("密码错误！");
            return;
        }
        try {
            student.setPassword(InputValidator.confirmPassword());
            if(studentService.modifyPassword(student)) {
                System.out.println("\n密码修改成功！");
            } else {
                System.out.println("修改失败，请检查输入数据");
            }
        } catch (Exception e) {
            System.out.println("修改失败：" + e.getMessage());
        }
    }

}
